//
//  DeviceInfoVc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/18.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "DeviceInfoVc.h"

#import "Tuichu_quren_popView.h"
#import "MMAlertView.h"
@interface DeviceInfoVc ()
@property (weak, nonatomic) IBOutlet UIImageView *iconIv;
@property (weak, nonatomic) IBOutlet UILabel *name_lb;
@property (weak, nonatomic) IBOutlet UILabel *shebei_id_lb;
@property (weak, nonatomic) IBOutlet UILabel *chanpin_id_lb;
@property (weak, nonatomic) IBOutlet UILabel *chanpin_xinghao_lb;
@property (weak, nonatomic) IBOutlet UILabel *tongxun_fangshi_lb;

@end

@implementation DeviceInfoVc
- (IBAction)yichu_action:(UIButton *)sender {
    sender.userInteractionEnabled = NO;
    
    
    QMUIDialogViewController *dialogViewController = [[QMUIDialogViewController alloc] init];
    dialogViewController.title = @"解除绑定";
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
    contentView.backgroundColor = UIColorWhite;
    UILabel *label = [[UILabel alloc] qmui_initWithFont:UIFontMake(14) textColor:UIColorBlack];
    label.text = [NSString stringWithFormat:@"是否解除 %@ 的绑定", self.entity.name];
    label.frame = CGRectMake(10, 0, CGRectGetWidth(contentView.frame) - 20, QMUIViewSelfSizingHeight);
    label.numberOfLines = 0;
    label.center = CGPointMake(CGRectGetWidth(contentView.bounds) / 2.0, CGRectGetHeight(contentView.bounds) / 2.0);
    [label sizeToFit];

    [contentView addSubview:label];
    dialogViewController.contentView = contentView;
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogViewController *aDialogViewController) {
        [aDialogViewController hide];
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:self.entity.device_id forKey:@"device_id"];
        [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
        
        [PPHTTPRequest req:@"api/App/deactive" parameters:dict success:^(id response) {
            NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
            if ([error_code isEqualToString:@"0"]) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
            
            else
                //            if ([error_code isEqualToString:@""])
            {
                
                [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
                
            }
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:[error localizedDescription]];
            
        }];
    }];
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    
    [dialogViewController show];

    sender.userInteractionEnabled = YES;

    
}
- (IBAction)changeNameAction:(UIButton *)sender {
    
    __weak __typeof(self) weakSelf = self;

    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = @"设备重命名";
    [dialogViewController addTextFieldWithTitle:nil configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = @"请输入新名称";
        textField.text = weakSelf.entity.name;
        //        textField.maximumTextLength = 10;
    }];
    dialogViewController.shouldEnableSubmitButtonBlock = ^BOOL(__kindof QMUIDialogTextFieldViewController * _Nonnull aDialogViewController) {
        if ([aDialogViewController.textFields.firstObject.text isEqualToString:weakSelf.entity.name] || aDialogViewController.textFields.firstObject.text.length == 0) {
            return NO;
        }
        return YES;
        
    };
    //    dialogViewController.shouldManageTextFieldsReturnEventAutomatically = YES;// 让键盘的 Return 键也能触发确定按钮的事件。这个属性默认就是 YES，这里为写出来只是为了演示
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(__kindof QMUIDialogTextFieldViewController * _Nonnull dialogViewController) {
        
        NSString *text = dialogViewController.textFields.firstObject.text;
        //        [QMUITips showSucceed:@"提交成功" inView:self.view hideAfterDelay:1.2];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
        
        
        [dict setObject:app_code forKey:@"app_code"];
        [dict setObject:weakSelf.entity.device_id forKey:@"device_id"];
        [dict setObject:text forKey:@"name"];
        
        [PPHTTPRequest req:@"api/App/modifyDeviceInfo" parameters:dict success:^(id response) {
            NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
            if ([error_code isEqualToString:@"0"]) {
                weakSelf.name_lb.text = text;
                if (self.blk) {
                    self.blk(text);
                }
            }
            
            else
                //            if ([error_code isEqualToString:@""])
            {
                
                [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
                
            }
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:[error localizedDescription]];
            
        }];
        [dialogViewController hide];
    }];
    [dialogViewController show];

   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设备信息";
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:self.entity.icon]];
    self.name_lb.text = self.entity.name;
    self.shebei_id_lb.text = self.entity.device_id;
    self.chanpin_id_lb.text = self.entity.product_id;
    self.tongxun_fangshi_lb.text = self.entity.communication;
    if (self.entity.is_virtual) {
        self.shebei_id_tip_lb.text = @"设备ID:";
        self.chanpin_xinghao_lb.text = self.entity.device_type.length == 0 ? self.entity.model : self.entity.device_type;

    }else{
        self.shebei_id_tip_lb.text = @"虚拟ID:";
        self.chanpin_xinghao_lb.text = self.entity.device_type.length == 0 ? self.entity.model : self.entity.device_type;


    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
